﻿using System;
using System.Collections.Generic;
using System.Linq;
using YJKSparseDistance.ModelLayer;
using YJKSparseDisOfDoor.ModelLayer;
using YJKGeometryFuncs;
using YJKInspectModelFunc;
using YJKInspectModelFunc.ModelLayer;

namespace YJKSparseDisOfDoor
{
	internal class EvacuationDoorDistance
	{
		public List<RoomPacker_EvacuationDistance> LstAllRoom { get; set; }

		public Dictionary<string, List<YJKSparseDisOfDoor.ModelLayer.ResultBean>> LstAllResult { get; set; }

		public EvacuationDoorDistance(List<RoomPacker_EvacuationDistance> lstAllRoom)
		{
			this.LstAllRoom = lstAllRoom;
			this.m_LstAllResult = new List<YJKSparseDisOfDoor.ModelLayer.ResultBean>();
			this.LstAllResult = new Dictionary<string, List<YJKSparseDisOfDoor.ModelLayer.ResultBean>>();
		}

		private bool DisDetection()
		{
			if (this.LstAllRoom.Count == 0)
			{
				return false;
			}
			double maxValue = double.MaxValue;
			string text = "";
			string text2 = "";
			DoorDistanceMatrix doorDistanceMatrix = new DoorDistanceMatrix();
			foreach (RoomPacker_EvacuationDistance roomPacker_EvacuationDistance in this.LstAllRoom)
			{
				if (roomPacker_EvacuationDistance.ThisRoom.Area >= double.Parse(Resource.MinRoomArea) || !roomPacker_EvacuationDistance.ThisRoom.Name.Contains(Resource.RoomLimit))
				{
					List<List<BaseDoorPacker>> groupedEvacuationDoors = roomPacker_EvacuationDistance.GetGroupedEvacuationDoors();
					if (groupedEvacuationDoors.Count != 0)
					{
						foreach (List<BaseDoorPacker> list in groupedEvacuationDoors)
						{
							if (list.Count >= 2)
							{
								for (int i = 0; i < list.Count - 1; i++)
								{
									BaseDoorPacker baseDoorPacker = list[i];
									for (int j = i + 1; j < list.Count; j++)
									{
										maxValue = double.MaxValue;
										BaseDoorPacker baseDoorPacker2 = list[j];
										Utility.GetRoomNameAndFloor(roomPacker_EvacuationDistance, baseDoorPacker, ref text, ref text2);
                                        if (YJKInspectModelFunc.Utility.CalTowDoorMinDistance(baseDoorPacker, baseDoorPacker2, ref maxValue))
										{
											RoomDoor inputStartDoor = new RoomDoor(roomPacker_EvacuationDistance, baseDoorPacker);
											RoomDoor inputEndDoor = new RoomDoor(roomPacker_EvacuationDistance, baseDoorPacker2);
											doorDistanceMatrix.AddValue(inputStartDoor, inputEndDoor, maxValue);
										}
									}
								}
							}
						}
					}
				}
			}
			doorDistanceMatrix.Update();
			this.InitResultBeanList(doorDistanceMatrix);
			return true;
		}

		private void InitResultBeanList(DoorDistanceMatrix doorDisMatrix)
		{
			string roomName = "";
			string floorName = "";
			double val = double.Parse(Resource.MinDoorDistance) * 1000.0 / 304.8;
			double val2 = double.Parse(Resource.DistanceStandardValue) * 1000.0 / 304.8;
			foreach (KeyValuePair<RoomDoor, Dictionary<RoomDoor, KeyValuePair<RoomDoor, double>>> keyValuePair in doorDisMatrix.DicMatrix)
			{
				foreach (KeyValuePair<RoomDoor, KeyValuePair<RoomDoor, double>> keyValuePair2 in keyValuePair.Value)
				{
					if (!Geometry.LessThan(keyValuePair2.Value.Value, val) && Geometry.LessThan(keyValuePair2.Value.Value, val2))
					{
						RoomDoor key = keyValuePair2.Value.Key;
						RoomDoor key2 = keyValuePair2.Key;
						double value = keyValuePair2.Value.Value;
						KeyValuePair<RoomDoor, RoomDoor> value2 = new KeyValuePair<RoomDoor, RoomDoor>(key, key2);
						if (!this.m_ExistedResultBean.Contains(value2))
						{
							Utility.GetRoomNameAndFloor(key.DoorInRoom, key.Door, ref roomName, ref floorName);
							YJKSparseDisOfDoor.ModelLayer.ResultBean resultBean = new YJKSparseDisOfDoor.ModelLayer.ResultBean(key.Door, key2.Door, roomName, floorName, value);
							if (!resultBean.IfTrust())
							{
								this.m_LstAllResult.Add(resultBean);
								this.m_ExistedResultBean.Add(key, key2);
							}
						}
					}
				}
			}
			this.m_LstAllResult = this.m_LstAllResult.Distinct<YJKSparseDisOfDoor.ModelLayer.ResultBean>().ToList<YJKSparseDisOfDoor.ModelLayer.ResultBean>();
		}

		private void RemoveTrustData()
		{
			foreach (YJKSparseDisOfDoor.ModelLayer.ResultBean resultBean in this.m_LstAllResult)
			{
				if (!resultBean.IfTrust())
				{
					this.AddValue(resultBean);
				}
			}
		}

		private void AddValue(YJKSparseDisOfDoor.ModelLayer.ResultBean input)
		{
			if (!this.LstAllResult.Keys.Contains(input.FloorName))
			{
				this.LstAllResult.Add(input.FloorName, new List<YJKSparseDisOfDoor.ModelLayer.ResultBean>());
			}
			this.LstAllResult[input.FloorName].Add(input);
		}

		public bool YJKSparseDisOfDoor()
		{
			if (!this.DisDetection())
			{
				return false;
			}
			this.RemoveTrustData();
			return true;
		}

		private List<YJKSparseDisOfDoor.ModelLayer.ResultBean> m_LstAllResult;

		private Dictionary<RoomDoor, RoomDoor> m_ExistedResultBean = new Dictionary<RoomDoor, RoomDoor>();

		private double m_StandardValue = 5.0;
	}
}
